<template>
  <div>
    <mbao-x item1="商品管理" item2="商品添加"></mbao-x>
    <el-card class="Top">
      <el-alert
        title="添加商品信息"
        type="info"
        show-icon
        center
        :closable="false"
      >
      </el-alert>
      <el-steps
        :active="active * 1"
        finish-status="success"
        align-center
        class="Top"
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <el-form :model="form" :rules="rules" ref="form" class="Top">
        <el-tabs
          :tab-position="tabPosition"
          v-model="active"
          :before-leave="Tabs"
          @tab-click="jump"
        >
          <el-tab-pane label="用户管理">
            <!-- {{form.goods_name}} -->
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="form.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model="form.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model="form.goods_weight"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model="form.goods_number"></el-input>
            </el-form-item>
            <el-form-item label="商品分类" prop="fen">
              <el-cascader
                v-model="value"
                :options="options"
                :props="props"
                @change="handleChange"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品参数">
            <el-form-item
              v-for="(item, index) in Many"
              :key="index"
              :label="item.attr_name"
            >
              <el-checkbox-group v-model="checkList" class="margin">
                <el-checkbox
                  label="复选框 A"
                  v-for="(item1, index1) in item.attr_vals"
                  :key="index1"
                  border
                  checked
                >
                  {{ item1 }}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品属性">
            <el-form-item
              v-for="(item, index) in Only"
              :key="index"
              :label="item.attr_name"
            >
              <el-input v-model="item.attr_vals"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片">
            <el-upload
              action="https://www.liulongbin.top:8888/api/private/v1/upload"
              list-type="picture-card"
              :headers="headers"
              :on-remove="handleRemove"
              :on-success="Update"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容">
            <quill-editor
              ref="myQuillEditor"
              v-model="form.goods_introduce"
              @blur="onEditorBlur($event)"
              @focus="onEditorFocus($event)"
              @ready="onEditorReady($event)"
            />
            <el-button type="primary" class="Top" @click="Addgood"
              >添加商品</el-button
            >
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import MbaoX from '../components/MbaoX.vue'
export default {
  components: {
    MbaoX
  },
  data() {
    return {
      tabPosition: 'left',
      // 复选框数组
      checkList: [],
      Many: [],
      Only: [],
      active: '0',
      form: {
        goods_name: '',
        goods_cat: '',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
        goods_introduce: '',
        pics: [],
        attrs: []
      },
      value: [],
      options: [],
      props: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      rules: {
        goods_name: [
          { required: true, message: '请输入商品名', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入价格', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入重量', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入数量', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        fen: [{ required: true, message: '请选择分类', trigger: 'blur' }]
      },
      headers: {
        Authorization: sessionStorage.getItem('token')
      }
    }
  },
  computed: {
    Ids() {
      return this.value[this.value.length - 1]
    }
  },
  created() {
    this.getlist()
  },
  mounted() {},
  methods: {
    async getlist() {
      const res = await this.$http.Cate()
      console.log(res)
      this.options = res
      if (this.$route.query) {
        this.form = this.$route.query
      }
    },
    // 添加商品
    async Addgood() {
      const arr = this.Many.map((item) => {
        return {
          attr_id: item.attr_id,
          attr_value: item.attr_vals.join(',')
        }
      })
      const arr1 = this.Only.map((item) => {
        return {
          attr_id: item.attr_id,
          attr_value: item.attr_vals
        }
      })
      this.form.attrs = [...arr, ...arr1]
      this.form.goods_cat = this.value.join(',')
      console.log(this.form)
      if (!this.$route.query) {
        await this.$http.ADDGood(this.form)
      } else {
        const { goods_id } = this.$route.query
        // const id = goods_id
        console.log(this.form);
        await this.$http.editGood(goods_id, this.form)
      }

      // console.log(res)
      this.$router.go(-1)
    },
    // 上传图片
    Update(res) {
      // tmp_path
      this.form.pics.push({
        pic: res.data.tmp_path
      })

      console.log(res)
    },
    // 移除图片
    handleRemove(res) {
      const index = this.form.pics.findIndex((item) => {
        return item.pic === res.response.data.tmp_path
      })
      this.form.pics.splice(index, 1)
      console.log(this.form.pics)
      // console.log(index)
    },
    // 点击tab栏的时候
    async jump() {
      console.log(this.active)
      if (this.active === '1') {
        const res = await this.$http.CateTo(this.Ids, { sel: 'many' })
        console.log(res)
        res.forEach((item) => {
          item.attr_vals =
            this.value.length == 3 ? item.attr_vals.split(',') : ''
        })
        this.Many = res
      } else if (this.active === '2') {
        const res = await this.$http.CateTo(this.Ids, { sel: 'only' })
        console.log(res)
        this.Only = res
      }
    },
    handleChange(value) {
      console.log(value)
    },
    Tabs(active, old) {
      if (this.value.length !== 3) {
        this.$message.warning('请选择三级分类')
        return false
      }
    },
    onEditorBlur(quill) {
      console.log('editor blur!', quill)
    },
    onEditorFocus(quill) {
      console.log('editor focus!', quill)
    },
    onEditorReady(quill) {
      console.log('editor ready!', quill)
    },
    onEditorChange({ quill, html, text }) {
      console.log('editor change!', quill, html, text)
      this.content = html
    }
  }
}
</script>
<style >
.ql-editor {
  height: 300px !important;
}
</style>
<style scoped lang='scss'>
.Top {
  margin-top: 20px;
}
.margin {
  margin-top: 50px;
}
::v-deep .el-step__title {
  font-size: 12px;
}
::v-deep.ql-editor {
  height: 300px !important;
}
</style>
